package q1004_longestOnes;

public class Solution {
    /*
     最大连续1的个数 III
     滑动窗口典型题
     可以理解为窗口内对于0的容忍个数最大为k
     找最长窗口
     */
    public int longestOnes(int[] nums, int k) {
        int left = 0, ans = 0, leftCount = 0, rightCount = 0;
        for (int right = 0; right < nums.length; right++) {
            rightCount += 1 - nums[right];
            while (leftCount < rightCount - k) {
                leftCount += 1 - nums[left];
                ++left;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
